package com.pig4cloud.mall.product.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.mall.product.dto.PageParam;
import com.pig4cloud.mall.product.entity.SkuInfo;
import com.pig4cloud.mall.product.service.SkuInfoService;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * sku信息
 *
 * @author fangyu
 * @date 2020-11-25 14:59:10
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sku")
@Api(value = "sku", tags = "sku信息管理")
public class SkuInfoController {

	private final SkuInfoService skuInfoService;

	/**
	 * 通过spuId查询下面所有的sku信息
	 *
	 * @param spuId spuId
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/all/{spuId}")
	@PreAuthorize("@pms.hasPermission('product_list_get')")
	public R getAllBySpuId(@PathVariable("spuId") Long spuId, PageParam params) {
		System.out.println(params.toString());
		return skuInfoService.getAllBySpuId(spuId, params);
	}


	/**
	 * 分页查询
	 *
	 * @param page    分页对象
	 * @param skuInfo sku信息
	 * @return
	 */
	@ApiOperation(value = "分页查询", notes = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('product_skuinfo_get')")
	public R getSkuInfoPage(Page page, SkuInfo skuInfo) {
		return R.ok(skuInfoService.page(page, Wrappers.query(skuInfo)));
	}


	/**
	 * 通过id查询sku信息
	 *
	 * @param skuId id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{skuId}")
	@PreAuthorize("@pms.hasPermission('product_skuinfo_get')")
	public R getById(@PathVariable("skuId") Long skuId) {
		return R.ok(skuInfoService.getById(skuId));
	}

	/**
	 * 新增sku信息
	 *
	 * @param skuInfo sku信息
	 * @return R
	 */
	@ApiOperation(value = "新增sku信息", notes = "新增sku信息")
	@SysLog("新增sku信息")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('product_skuinfo_add')")
	public R save(@RequestBody SkuInfo skuInfo) {
		return R.ok(skuInfoService.save(skuInfo));
	}

	/**
	 * 修改sku信息
	 *
	 * @param skuInfo sku信息
	 * @return R
	 */
	@ApiOperation(value = "修改sku信息", notes = "修改sku信息")
	@SysLog("修改sku信息")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('product_skuinfo_edit')")
	public R updateById(@RequestBody SkuInfo skuInfo) {
		return R.ok(skuInfoService.updateById(skuInfo));
	}

	/**
	 * 通过id删除sku信息
	 *
	 * @param skuId id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除sku信息", notes = "通过id删除sku信息")
	@SysLog("通过id删除sku信息")
	@DeleteMapping("/{skuId}")
	@PreAuthorize("@pms.hasPermission('product_skuinfo_del')")
	public R removeById(@PathVariable Long skuId) {
		return R.ok(skuInfoService.removeById(skuId));
	}

}
